<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="lE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>23软件三班 2340303007 老木 轮播图</title>
</head>
<style>
    *{
    margin: 0;
    padding: 0;
    text-decoration: none;
    list-style: none;
}
#banner{
    width: 800px;
    height: 400px;
    border: 2px solid #999;
    overflow: hidden; 
    position: relative;
    margin: 250px auto;
}
#imglist{
    width: 4000px;
    height: 400px;
}
#imglist img{
    width: 800px;
    height: 400px;
}
#imglist li{
    float: left;
}
.prev{
    background: #666;
    width: 30px;
    height: 40px;
    color: #fff;
    text-align: center;
    line-height: 40px;
    position: absolute;
    left: 5px;
    top: 45%;
    cursor: pointer;
}
.next{
    background: #666;
    width: 30px;
    height: 40px;
    color: #fff;
    text-align: center;
    line-height: 40px;
    position: absolute;
    right: 5px;
    top: 45%;
    cursor: pointer;
}
#icolist{
    position: absolute;
    right: 10px;
    bottom: 10px;
}
#icolist li{
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: #999;
    text-align: center;
    line-height: 30px;
    color: #fff;
    float: left;
    margin-left: 5px;
    cursor: pointer;
}
</style>
<body>
    <div id="banner">
        <ul id="imglist">
            <li><img src="./图1.jpg" alt=""></li>
            <li><img src="./图2.jpg" alt=""></li>
            <li><img src="./图3.jpg" alt=""></li>
            <li><img src="./图4.jpg" alt=""></li>
            <li><img src="./图1.jpg" alt=""></li>
        </ul>
        <ul id="icolist">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <div class="prev">〈</div>
        <div class="next">〉</div>
    </div>
    <script>
    var esico = document.getElementById('icolist').getElementsByTagName('li');
var eicolist = document.querySelector('#icolist');
var eimglist = document.querySelector('#imglist');
var etest = document.querySelector('#test');
var eprev = document.querySelector('.prev');
var enext = document.querySelector('.next');
var left = 0;
var timer;
esico[2].style.backgroundColor = 'red';
run();
// alert(Math.floor(3190/800));
function run(){
    if (left <= -3200) {
        left = 0;
    }  
    var m = Math.floor(-left/800) ;
    imglist.style.marginLeft = left + 'px';
    var n = (left % 800 == 0) ? n = 1800 : n = 10;
    left -= 10;
    icochange(m);
    timer = setTimeout(run,n);
    }
    function icochange(m){
    for (let index = 0; index < esico.length; index++) {
        esico[index].style.backgroundColor = '';
    }
    if (m < esico.length) {
        esico[m].style.backgroundColor = 'red';
    }
    }
    function imgchange(n){
        let lt = - (n  * 800)
        imglist.style.marginLeft = lt + 'px';
        left = lt;
    }
    eprev.onclick = function(){
        let prevgo = Math.floor(-left/800)-1;
    if (prevgo == -1) {
        prevgo =3;
    }
        imgchange(prevgo);
        icochange(prevgo);
    }
    enext.onclick = function(){
        let nextgo = Math.floor(-left/800)+1;
    if (nextgo == 4) {
        nextgo =0;
    }
        imgchange(nextgo);
        icochange(nextgo);
    }
    eicolist.onclick = function(){
    var tg = event.target;
    let ico = tg.innerHTML - 1;
        imgchange(ico);
        icochange(ico);
    }
    eimglist.onmouseover = function(){
        clearTimeout(timer);
    }
    eimglist.onmouseout = function(){
        run();
    }
</script>
</body>
</html>